module.exports = {
  schedule: {
    cron: '0 0 * * * ?',
    type: 'worker',
    env: ['pachong'],
  },

  async task(ctx) {
    ctx.logger.info(`task_delivery 开始...`);

    const now = new Date();
    let currentHour = now.getHours();

    const alerts = await ctx.service.alertSvc.getAlertsToSendEmail(currentHour);

    for (const alert of alerts) {
      const content = await ctx.service.alertSvc.getMatchingTopics(alert.kws, alert.last_delivery_at);
      if (content.length > 0) {
        await ctx.service.mailSvc.sendDeliveryMail(alert.email, content);
        await ctx.service.alertSvc.updateLastDeliveryAt(alert.user_id, now);

      }
    }

    ctx.logger.info(`task_delivery 结束...`);
  },
};
